package com.rescueworkers.task;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.os.SystemClock;
import android.util.Log;

import com.fg114.main.service.dto.JsonPack;
import com.fg114.main.service.http.A57HttpApiV3;
import com.fg114.main.service.task.BaseTask;
import com.fg114.main.util.ActivityUtil;
import com.fg114.main.util.DialogUtil;
import com.fg114.main.util.SessionManager;
import com.fg114.main.util.SharedprefUtil;
import com.google.xiaomishujson.JsonArray;
import com.rescueworkers.Settings;
import com.rescueworkers.UserLoginActivity;
import com.rescueworkers.Db.GpsDb;
import com.rescueworkers.Db.TaskDb;
import com.rescueworkers.dto.Task;

public class UploadGpsInfoTask extends BaseTask{
	private Context context;
	private String gps = "";
	public UploadGpsInfoTask(String preDialogMessage, Context context) {
		super(preDialogMessage, context);
		this.context = context;
	}

	@Override
	public JsonPack getData() throws Exception {
		if (!Settings.DEBUG) {
			gps = GpsDb.getGpsForUpload();
			JSONObject jsonObj = new JSONObject();
			jsonObj.put("gps", gps);
			try {
				JSONArray array =  new JSONArray(gps);
				if(array == null || array.length() <=  0 ){
					return getTestData();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
            // 从任务状态切换列表中，获取最多10条信息，组装成一条数据上传
			return A57HttpApiV3.getInstance().postGpsInfo(gps);

		} else {
			return getTestData();
		}
	}

	@Override
	public void onStateFinish(JsonPack result) {
		//上传成功后，更新两张表中的上传状态
		JSONArray jsonArray;
		try {
			jsonArray = new JSONArray(gps);
			if(jsonArray != null && jsonArray.length() > 0 ){
				for(int i = 0 ;i<jsonArray.length() ;i++){
					JSONObject jsonobj = jsonArray.getJSONObject(i);
					if(jsonobj != null){
						if(jsonobj.has("uuid") ){
							String uuid = jsonobj.getString("uuid");
							ContentValues values = new ContentValues();
							values.put("uploadFlag", "1");
							Settings.dbHelper.getWritableDatabase().update("gpsInfo", values, "uuid = ? ", new String[]{uuid});
						}
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onStateError(JsonPack result) {
//		DialogUtil.showToast(context, result.getMsg());
		if(result != null && result.getRe() == Settings.CODE_TOKEN_INVALIDE){
			//退出app重新登陆
			SharedprefUtil.resetByKey(context, Settings.IS_LOGIN_KEY);
			SharedprefUtil.resetByKey(context, Settings.LOGIN_USER_INFO_KEY);
			System.exit(0);
//			ActivityUtil.jump(context, UserLoginActivity.class, 1,null,true);
		}
	}

	@Override
	public void onPreStart() {
		
	}
	
	private JsonPack getTestData() throws JSONException{
		//---------------------------测试数据
		SystemClock.sleep(500);
		String data="{}";
		JsonPack result=new JsonPack();
		result.setObj(new JSONObject(data));
		//-----------------------------------
		return result;
	}

}
